//
//  BKHistoryViewModel.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/6/1.
//  Copyright (c) 2015年 宏图教育. All rights reserved.
//

#import "BKHistoryViewModel.h"
#import <AFNetworking.h>

@implementation BKHistoryViewModel


+ (void)colleageWithProv:(NSString *)prov
                 success:(void (^)(NSArray *))success
                 failure:(void (^)(NSString *))failure{
    
    
    NSDictionary *provDic =  @{
                                        @"北京":@"1",
                                         @"天津":@"2",
                                         @"上海":@"3",
                                         @"重庆":@"4",
                                         @"河北":@"5",
                                         @"河南":@"6",
                                         @"山东":@"7",
                                         @"山西":@"8",
                                         @"安徽":@"9",
                                         @"江西":@"10",
                                         @"江苏":@"11",
                                         @"浙江":@"12",
                                         @"湖北":@"13",
                                         @"湖南":@"14",
                                         @"广东":@"15",
                                         @"广西":@"16",
                                         @"云南":@"17",
                                         @"贵州":@"18",
                                         @"四川":@"19",
                                         @"陕西":@"20",
                                         @"青海":@"21",
                                         @"宁夏":@"22",
                                         @"黑龙江":@"23",
                                         @"吉林":@"24",
                                         @"辽宁":@"25",
                                         @"西藏":@"26",
                                         @"新疆":@"27",
                                         @"内蒙古":@"28",
                                         @"海南":@"29",
                                         @"福建":@"30",
                                         @"甘肃":@"31"};
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"provid"] = provDic[prov]?:@"1";
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    [manager GET:@"http://www.baokaodaxue.com/bk/index/getcollege"
      parameters:params
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSDictionary *responseDic=[NSJSONSerialization
                                        JSONObjectWithData:responseObject
                                        options:NSJSONReadingMutableLeaves
                                        error:nil];
             if ([[responseDic objectForKey:@"code"] isEqualToString:@"200"]) {
                 
                 NSArray *dataArray = responseDic[@"extraData"];
                 
                 
                 success(dataArray);
                 
                 
             }else{
                 failure([responseDic objectForKey:@"message"]);
                 
             }
             
         }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             failure(@"网络连接失败");
         }];
    


}

+ (void)historyWithKaoQu:(NSString *)kq
                   wenli:(NSString *)wl
                    diqu:(NSString *)dq
                     cid:(NSString *)cid
                    pici:(NSString *)pc
                 success:(void (^)(NSDictionary *))success
                 failure:(void (^)(NSString *))failure{
    NSDictionary *provDic =  @{ @"北京":@"1",
                                @"天津":@"2",
                                @"上海":@"3",
                                @"重庆":@"4",
                                @"河北":@"5",
                                @"河南":@"6",
                                @"山东":@"7",
                                @"山西":@"8",
                                @"安徽":@"9",
                                @"江西":@"10",
                                @"江苏":@"11",
                                @"浙江":@"12",
                                @"湖北":@"13",
                                @"湖南":@"14",
                                @"广东":@"15",
                                @"广西":@"16",
                                @"云南":@"17",
                                @"贵州":@"18",
                                @"四川":@"19",
                                @"陕西":@"20",
                                @"青海":@"21",
                                @"宁夏":@"22",
                                @"黑龙江":@"23",
                                @"吉林":@"24",
                                @"辽宁":@"25",
                                @"西藏":@"26",
                                @"新疆":@"27",
                                @"内蒙古":@"28",
                                @"海南":@"29",
                                @"福建":@"30",
                                @"甘肃":@"31"};
    NSDictionary *piciDic = @{@"提前批":@"5",
                              @"本科一批":@"11",
                              @"本科二批":@"12",
                              @"本科三批":@"13",
                              @"高职专科":@"20"};


    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"kq"] = provDic[kq]?:@"";
    params[@"wl"]= [wl isEqual:@"文科"]?@"1":@"2";
    params[@"dq"] = provDic[dq]?:@"";
    params[@"cid"] = cid?:@"";
    params[@"pc"] = piciDic[pc]?:@"";
  
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    [manager GET:@"http://www.baokaodaxue.com/bk/mobile/fenshu"
      parameters:params
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSDictionary *responseDic=[NSJSONSerialization
                                        JSONObjectWithData:responseObject
                                        options:NSJSONReadingMutableLeaves
                                        error:nil];
             if ([[responseDic objectForKey:@"code"] isEqualToString:@"200"]) {
                 
                 NSDictionary *dic = responseDic[@"extraData"];
                 
                 
                 success(dic);
                 
                 
             }else{
                 failure([responseDic objectForKey:@"message"]);
                 
             }
             
         }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             failure(@"网络连接失败");
         }];
    
}


+ (void)historScoreWithCid:(NSString *)cid
                        kq:(NSString *)kq
                   success:(void (^)(NSDictionary *))success
                   failure:(void (^)(NSString *))failure{
    NSDictionary *provDic =  @{ @"北京":@"1",
                                @"天津":@"2",
                                @"上海":@"3",
                                @"重庆":@"4",
                                @"河北":@"5",
                                @"河南":@"6",
                                @"山东":@"7",
                                @"山西":@"8",
                                @"安徽":@"9",
                                @"江西":@"10",
                                @"江苏":@"11",
                                @"浙江":@"12",
                                @"湖北":@"13",
                                @"湖南":@"14",
                                @"广东":@"15",
                                @"广西":@"16",
                                @"云南":@"17",
                                @"贵州":@"18",
                                @"四川":@"19",
                                @"陕西":@"20",
                                @"青海":@"21",
                                @"宁夏":@"22",
                                @"黑龙江":@"23",
                                @"吉林":@"24",
                                @"辽宁":@"25",
                                @"西藏":@"26",
                                @"新疆":@"27",
                                @"内蒙古":@"28",
                                @"海南":@"29",
                                @"福建":@"30",
                                @"甘肃":@"31"};
//    NSDictionary *piciDic = @{@"提前批":@"5",
//                              @"本科一批":@"11",
//                              @"本科二批":@"12",
//                              @"本科三批":@"13",
//                              @"专科一批":@"21",
//                              @"专科二批":@"22"};
    
    
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    if ([cid isKindOfClass:[NSString class]]&& [cid length]>0) {
        params[@"cid"] = cid;
    }
    if ([kq isKindOfClass:[NSString class]]&&[kq length]>0) {
        params[@"kq"] = provDic[kq];
    }
    
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    [manager GET:@"http://www.baokaodaxue.com/bk/mobile/fenshu"
      parameters:params
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSDictionary *responseDic=[NSJSONSerialization
                                        JSONObjectWithData:responseObject
                                        options:NSJSONReadingMutableLeaves
                                        error:nil];
             if ([[responseDic objectForKey:@"code"] isEqualToString:@"200"]) {
                 
                 NSDictionary *dic = responseDic[@"extraData"];
                 
                 
                 success(dic);
                 
                 
             }else{
                 failure([responseDic objectForKey:@"message"]);
                 
             }
             
         }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             failure(@"网络连接失败");
         }];
    
    
    


}


+ (void)liuxueWithCid:(NSString *)cid provid:(NSString *)provid
              success:(void (^)(NSArray *))success
              failure:(void (^)(NSString *))failure{

    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    

    if ([cid isKindOfClass:[NSString class]]&&[cid length]>0) {
        params[@"cid"] = cid;
    }
    
    NSDictionary *dic = @{ @"北京":@"1",
                           @"天津":@"2",
                           @"上海":@"3",
                           @"重庆":@"4",
                           @"河北":@"5",
                           @"河南":@"6",
                           @"山东":@"7",
                           @"山西":@"8",
                           @"安徽":@"9",
                           @"江西":@"10",
                           @"江苏":@"11",
                           @"浙江":@"12",
                           @"湖北":@"13",
                           @"湖南":@"14",
                           @"广东":@"15",
                           @"广西":@"16",
                           @"云南":@"17",
                           @"贵州":@"18",
                           @"四川":@"19",
                           @"陕西":@"20",
                           @"青海":@"21",
                           @"宁夏":@"22",
                           @"黑龙江":@"23",
                           @"吉林":@"24",
                           @"辽宁":@"25",
                           @"西藏":@"26",
                           @"新疆":@"27",
                           @"内蒙古":@"28",
                           @"海南":@"29",
                           @"福建":@"30",
                           @"甘肃":@"31"};
    params[@"provid"] = dic[provid];
    
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    [manager GET:@"http://www.baokaodaxue.com/bk/mobile/cjihua"
      parameters:params
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSDictionary *responseDic=[NSJSONSerialization
                                        JSONObjectWithData:responseObject
                                        options:NSJSONReadingMutableLeaves
                                        error:nil];
             if ([[responseDic objectForKey:@"code"] isEqualToString:@"200"]) {
                 
                 NSArray *data = responseDic[@"extraData"];
                 
                 
                 success(data);
                 
                 
             }else{
                 failure([responseDic objectForKey:@"message"]);
                 
             }
             
         }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             failure(@"网络连接失败");
         }];

    
}

+ (void)zhaoshengWithCid:(NSString *)cid provid:(NSString *)provid success:(void (^)(NSArray *))success failure:(void (^)(NSString *))failure{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    if ([cid isKindOfClass:[NSString class]]&&[cid length]>0) {
        params[@"cid"] = cid;
    }
    if ([provid isKindOfClass:[NSString class]]&&[provid length]>0) {
        params[@"provid"] = provid;
    }
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    [manager GET:@"http://www.baokaodaxue.com/bk/mobile/cjihua"
      parameters:params
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSDictionary *responseDic=[NSJSONSerialization
                                        JSONObjectWithData:responseObject
                                        options:NSJSONReadingMutableLeaves
                                        error:nil];
             if ([[responseDic objectForKey:@"code"] isEqualToString:@"200"]) {
                 
                 NSArray *data = responseDic[@"extraData"];
                 
                 
                 success(data);
                 
                 
             }else{
                 failure([responseDic objectForKey:@"message"]);
                 
             }
             
         }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             failure(@"网络连接失败");
         }];
    

    
}

+ (void)zhusuWithCid:(NSString *)cid success:(void (^)(NSArray *))success failure:(void (^)(NSString *))failure{

    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    if ([cid isKindOfClass:[NSString class]]&&[cid length]>0) {
        params[@"cid"] = cid;
    }
    params[@"provid"] = @"1";
    
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    [manager GET:@"http://www.baokaodaxue.com/bk/mobile/cjihua"
      parameters:params
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSDictionary *responseDic=[NSJSONSerialization
                                        JSONObjectWithData:responseObject
                                        options:NSJSONReadingMutableLeaves
                                        error:nil];
             if ([[responseDic objectForKey:@"code"] isEqualToString:@"200"]) {
                 
                 NSArray *data = responseDic[@"extraData"];
                 
                 
                 success(data);
                 
                 
             }else{
                 failure([responseDic objectForKey:@"message"]);
                 
             }
             
         }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             failure(@"网络连接失败");
         }];
    
    

    
}


+ (void)xuefeiWithCid:(NSString *)cid
                 page:(NSString *)page
              success:(void(^)(NSArray *dataArray))success
              failure:(void(^)(NSString *message))failure{

    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    if ([cid isKindOfClass:[NSString class]]&&[cid length]>0) {
        params[@"cid"] = cid;
    }
    params[@"page"] = @"1";
    params[@"len"] =@10000;
    
    
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    [manager GET:@"http://www.baokaodaxue.com/bk/mobile/cxuefei"
      parameters:params
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSDictionary *responseDic=[NSJSONSerialization
                                        JSONObjectWithData:responseObject
                                        options:NSJSONReadingMutableLeaves
                                        error:nil];
             if ([[responseDic objectForKey:@"code"] isEqualToString:@"200"]) {
                 
                 NSArray *data = responseDic[@"extraData"][@"info"];
                 
                 
                 success(data);
                 
                 
             }else{
                 failure([responseDic objectForKey:@"message"]);
                 
             }
             
         }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             failure(@"网络连接失败");
         }];
    
}
+ (void)shoucangWithCid:(NSString *)cid
                success:(void (^)())success
                failure:(void (^)(NSString *))failure{

    
    
    
    
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.baokaodaxue.com/bk/mobile/ioscshoucang?cid=%@",cid]];
    NSString *post = [NSString stringWithFormat:@"cid=%@",cid];
    
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    

 
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:postData];
    [request setTimeoutInterval:10.0];
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    
    NSDictionary *cookieDic = [userDefault objectForKey:@"Cookie"];
    NSString *cookieString = [NSString stringWithFormat:@"%@=%@",cookieDic[@"name"],cookieDic[@"value"]];
    
    [request setValue:cookieString forHTTPHeaderField:@"Cookie"];
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    
   
    
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:queue
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
                               if (error) {
                                   failure(@"网络连接失败");
                               }else{
                                   NSDictionary *responseDic=[NSJSONSerialization JSONObjectWithData:data
                                                                                             options:NSJSONReadingMutableLeaves
                                                                                               error:nil];
                                   
                                   if ([[responseDic objectForKey:@"code"] isEqualToString:@"200"]) {
                                     
                                       success();
                                       
                                       
                                   }else{
                                       
                                       NSString *messge = [responseDic objectForKey:@"message"];
                                       if ([messge isEqualToString:@"请先登录"]) {
                                           NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                                           [userDefaults removeObjectForKey:@"Cookie"];
                                           
                                       }
                                       failure([responseDic objectForKey:@"message"]);
                                   }
                               }
                           }];
    
   
}
+(Boolean) isEmptyOrNull:(NSString *) str {
    if (!str) {
        // null object
        return true;
    } else {
        NSString *trimedString = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        if ([trimedString isKindOfClass:[NSString class]]&&[trimedString length] == 0) {
            // empty string
            return true;
        } else {
            // is neither empty nor null
            return false;
        }
    }
}

+ (void)jiangxuejinWithCid:(NSString *)cid success:(void (^)(NSArray *))success failure:(void (^)(NSString *))failure{

    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    if ([cid isKindOfClass:[NSString class]]&&[cid length]>0) {
        params[@"cid"] = cid;
    }
    
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    [manager GET:@"http://www.baokaodaxue.com/bk/mobile/cjiangxuejin2"
      parameters:params
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSDictionary *responseDic=[NSJSONSerialization
                                        JSONObjectWithData:responseObject
                                        options:NSJSONReadingMutableLeaves
                                        error:nil];
             if ([[responseDic objectForKey:@"code"] isEqualToString:@"200"]) {
                 
                 NSArray *data = responseDic[@"extraData"];
                 
                 
                 success(data);
                 
                 
             }else{
                 failure([responseDic objectForKey:@"message"]);
                 
             }
             
         }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             failure(@"网络连接失败");
         }];
    
    

}
@end
